package com.esri.core.tasks.na;

import com.esri.core.internal.tasks.d.a;
import com.esri.core.internal.tasks.e;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/* loaded from: classes.dex */
public class ClosestFacilityTask {
    String _url;
    UserCredentials credentials;
    private long mAttributeHelper = 0;
    private NetworkDescription mDescription = null;

    public ClosestFacilityTask(String str) {
        this._url = str;
    }

    public ClosestFacilityTask(String str, UserCredentials userCredentials) {
        this._url = str;
        this.credentials = userCredentials != null ? userCredentials.getCopy() : null;
    }

    public ClosestFacilityParameters getDefaultParameters() throws Exception {
        a aVar = new a(null, this._url, this.credentials);
        ClosestFacilityParameters closestFacilityParameters = new ClosestFacilityParameters();
        closestFacilityParameters._params = aVar.a();
        return closestFacilityParameters;
    }

    public ClosestFacilityResult solve(ClosestFacilityParameters closestFacilityParameters) throws Exception {
        a aVar = new a(closestFacilityParameters.getParams(), this._url, this.credentials);
        initializeHelper();
        String[] strArr = new String[3];
        double[] dArr = {1.0d, 1.0d, 1.0d};
        String[] augmentAttributes = LocalRouteTask.augmentAttributes(this.mAttributeHelper, closestFacilityParameters.getImpedanceAttributeName(), closestFacilityParameters.getAccumulateAttributeNames(), strArr, dArr);
        if (strArr[0] != null && augmentAttributes != null) {
            closestFacilityParameters.setImpedanceAttributeName(strArr[0]);
            closestFacilityParameters.setAccumulateAttributeNames(augmentAttributes);
        }
        ClosestFacilityResult execute = aVar.execute();
        for (Route route : execute.getRoutes()) {
            route.updateConvenience(strArr[1], strArr[2], dArr[0], dArr[1], dArr[2]);
        }
        return execute;
    }

    public Future<ClosestFacilityResult> solve(final ClosestFacilityParameters closestFacilityParameters, final CallbackListener<ClosestFacilityResult> callbackListener) {
        return e.c.submit(new Callable<ClosestFacilityResult>() { // from class: com.esri.core.tasks.na.ClosestFacilityTask.1
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.util.concurrent.Callable
            public ClosestFacilityResult call() {
                ClosestFacilityResult closestFacilityResult = null;
                try {
                    closestFacilityResult = ClosestFacilityTask.this.solve(closestFacilityParameters);
                } catch (Exception e) {
                    CallbackListener callbackListener2 = callbackListener;
                    if (callbackListener2 != null) {
                        callbackListener2.onError(e);
                    }
                }
                if (Thread.currentThread().isInterrupted()) {
                    return closestFacilityResult;
                }
                CallbackListener callbackListener3 = callbackListener;
                if (callbackListener3 != null) {
                    callbackListener3.onCallback(closestFacilityResult);
                }
                return closestFacilityResult;
            }
        });
    }

    protected void finalize() throws Throwable {
        LocalRouteTask.releaseAttributeHelper(this.mAttributeHelper);
        this.mAttributeHelper = 0L;
        super.finalize();
    }

    private void initializeHelper() throws Exception {
        if (this.mDescription != null) {
            return;
        }
        NetworkDescription c = new a(null, this._url, this.credentials).c();
        this.mDescription = c;
        List<CostAttribute> costAttributes = c.getCostAttributes();
        String[] strArr = new String[costAttributes.size()];
        String[] strArr2 = new String[costAttributes.size()];
        for (int i = 0; i < costAttributes.size(); i++) {
            strArr[i] = costAttributes.get(i).getName();
            strArr2[i] = costAttributes.get(i).getUnits().name();
        }
        this.mAttributeHelper = LocalRouteTask.createAttributeHelper(0L, strArr, strArr2, this.mDescription.mDefaultImpedance, (String[]) this.mDescription.mDefaultAccumulates.toArray(new String[this.mDescription.mDefaultAccumulates.size()]), this.mDescription.mDefaultDirectionsTimeAttribute);
    }
}
